package com.czwx.bee.business.manager.modular.sys.controller;

import com.czwx.bee.base.common.dto.ResponseDto;
import com.czwx.bee.base.common.exception.BizExceptionEnum;
import com.czwx.bee.base.common.interfaces.BpUserTypeEnum;
import com.czwx.bee.base.web.common.BaseController;
import com.czwx.bee.business.manager.modular.sys.model.Role;
import com.czwx.bee.business.manager.modular.sys.model.SecurityUser;
import com.czwx.bee.business.manager.modular.sys.model.query.RoleQuery;
import com.czwx.bee.business.manager.modular.sys.service.RoleService;
import com.czwx.bee.business.manager.utils.SecurityUtil;
import io.swagger.annotations.*;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Api(value = "RoleController",tags = "角色控制器")
@RestController
@RequestMapping("role")
@Validated
public class RoleController extends BaseController {


    @Autowired
    RoleService roleService;

    @PostMapping("list")
    @ApiOperation(value = "角色列表", notes = "角色列表", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "请输入凭证", required = true, dataType = "String", paramType = "header"),
            @ApiImplicitParam(name = "roleQuery", value = "请输入权限信息", required = true, dataType = "RoleQuery", paramType = "body") })
    @ApiResponses(
            @ApiResponse(code = 200,response = Role.class,message = "data：中返回list")
    )
    public ResponseDto<Role> roleList(@RequestBody @Validated RoleQuery roleQuery
    ) throws Exception {
             ResponseDto rs=new ResponseDto<Role>(BizExceptionEnum.SUCCESSFUL_OPERATION);
             PageQuery query=  new PageQuery(roleQuery.getPage(),roleQuery.getSize(),objectToMap(roleQuery));
             roleService.getPageList(query);
             rs.setData(query);
            return rs;
    }

    @PostMapping("save")
    @ApiOperation(value = "角色添加或修改", notes = "角色添加或修改", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "请输入凭证", required = true, dataType = "String", paramType = "header"),
            @ApiImplicitParam(name = "role", value = "请输入角色信息", required = true, dataType = "Role", paramType = "body") })
    public ResponseDto saveOrUpdate(@RequestBody @Validated Role role){
        ResponseDto rs=new ResponseDto(BizExceptionEnum.SUCCESSFUL_OPERATION);
        roleService.saveOrUpdate(role);
        return rs;
    }

    @PostMapping("auth_resource")
    @ApiOperation(value = "角色授权菜单资源", notes = "角色授权菜单资源", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "请输入凭证", required = true, dataType = "String", paramType = "header"),
            @ApiImplicitParam(name = "roleId", value = "请输入角色id", required = true, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "resourceIds", value = "请输入菜单资源ids", required = true, dataType = "String", paramType = "query")})

    public ResponseDto authRoleResrouce(@NotNull(message = "roleId不能为空") Long roleId,@NotNull(message = "resourceIds") String resourceIds){
        ResponseDto rs=new ResponseDto(BizExceptionEnum.SUCCESSFUL_OPERATION);
        String[] ids=resourceIds.split(",");
        roleService.AuthRoleResource(roleId,ids);
        return rs;
    }

    @PostMapping("role_resources")
    @ApiOperation(value = "查询角色相关资源（加选中数组）", notes = "查询角色相关资源（资源树 加选中数组）", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "请输入凭证", required = true, dataType = "String", paramType = "header"),
            @ApiImplicitParam(name = "roleId", value = "请输入角色id", required = true, dataType = "Long", paramType = "query"),
          })

    public ResponseDto getRoleResources(@NotNull(message = "roleId不能为空") Long roleId){
        ResponseDto rs=new ResponseDto(BizExceptionEnum.SUCCESSFUL_OPERATION);
        rs.setData(roleService.roleTreeWithIds(roleId));
        return rs;
    }

    @PostMapping("del")
    @ApiOperation(value = "删除资源", notes = "删除资源", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "请输入凭证", required = true, dataType = "String", paramType = "header"),
            @ApiImplicitParam(name = "roleId", value = "请输入角色id", required = true, dataType = "Long", paramType = "query"),
    })
    public ResponseDto del(@NotNull(message = "roleId不能为空") Long roleId){
        ResponseDto rs=new ResponseDto(BizExceptionEnum.SUCCESSFUL_OPERATION);
        Role role=new Role();
        role.setId(roleId);
        role.setStatus(-1);
        roleService.update(role);
        return rs;
    }

    @PostMapping("role_select")
    @ApiOperation(value = "获取角色多选框选项", notes = "获取角色多选框选项", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "请输入凭证", required = true, dataType = "String", paramType = "header"),
    })
    public ResponseDto roleSelect(){
        ResponseDto rs=new ResponseDto(BizExceptionEnum.SUCCESSFUL_OPERATION);
        SecurityUser securityUser=SecurityUtil.getSecurityUser();
        List<Map> roles= roleService.findUserRoleByUid(securityUser.getId());
        rs.setData(roles);
        return rs;
    }
}
